﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using ReflectionStudio.Controls;
using System.Windows.Input;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAAV25 : DocumentBase
    {
        #region-------------Properties-----------------------
        bool IsLoaded = false;
        DataRow LastRow = null;
        DataSet SetMap = null;
        string Mandant = "00";
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        YCAAV25Data m_Data, m_DataX;
        public DataSet dsParamaters;
        YCAAV25Report m_rpt;
        DataTable tbUser = null;
        bool IsSaved = false;
        public string Qty
        {
            get
            {
                return txtQty.Text;
            }
            set
            {
                txtQty.Text = value;
            }
        }
        public string LO
        {
            get
            {
                return txtLO.Text;
            }
        }
        public string LB
        {
            get
            {
                return txtLB.Text;
            }
        }
        public string LP
        {
            get
            {
                return txtLP.Text;
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        string user = "";
        #endregion
        public YCAAV25()
        {
            InitializeComponent();
            this.Name = "生产入库";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            tbUser = (DataTable)dicParamater["USERRIGHT"];
            user = (string)dicParamater["User"];
            m_Data = new YCAAV25Data(OracleDB, proxy);
            m_Data.UserID = user;
            ReSetEnv();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                vwABPOS.View.CurrentChanged += vwABPOS_CurrentChanged;
                txtABPART.Text = "24106090095";
                EventDispatcher.Instance.OnMessage += new EventHandler<MessageEventArgs>(_LogExplorerDock.OnMessage);
                RefreshFile();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }

        void vwABPOS_CurrentChanged(object sender, EventArgs e)
        {
            if (dwABPOS == null)
                return;
            DataSet SResult = m_Data.LoadData(dwABPOS);
            SetProd.Clear();
            SetProd.Merge(SResult);
            FilterWD();
            FilterFD();

        }
        public void ReSetEnv()
        {
            DataSet setResult = new DataSet();
            try
            {
                setResult = m_Data.LoadEnv();
                SetEnv.Clear();
                SetEnv.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #region=====vwABPOS=======
        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if (SetData.ABPOS.Rows.Count == 0)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        BindingListCollectionView cvWDECK
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgWDECK.ItemsSource);
            }
        }
        BindingListCollectionView cvFDECK
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgFDECK.ItemsSource);
            }
        }
        #endregion
        #region=====vwABPAUF=======
        CollectionViewSource vwABPAUF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPAUF");
            }
        }
        DataRow dwABPAUF
        {
            get
            {
                if (vwABPAUF.View == null)
                    return null;
                if ((DataRowView)vwABPAUF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPAUF.View.CurrentItem).Row;
            }
        }
        #endregion

        #region=====vwLIEFDAT=======
        CollectionViewSource vwLIEFDAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwLIEFDAT");
            }
        }
        DataRow dwLIEFDAT
        {
            get
            {
                if (vwLIEFDAT.View == null)
                    return null;
                if ((DataRowView)vwLIEFDAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwLIEFDAT.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwFSSTAMM=======
        CollectionViewSource vwFSSTAMM
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFSSTAMM");
            }
        }
        DataRow dwFSSTAMM
        {
            get
            {
                if (vwFSSTAMM.View == null)
                    return null;
                if ((DataRowView)vwFSSTAMM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFSSTAMM.View.CurrentItem).Row;
            }
        }
        #endregion

        private void FilterWD()
        {
            string filter = "WDAUFKRS='" + dwABPOS["ABPAUFKRS"].ToString() + "' AND WDAUFLFD ='" + dwABPOS["ABPAUFLFD"].ToString() + "' AND WDAUFPOS='" + dwABPOS["ABPAUFPOS"].ToString() + "'";
            cvWDECK.CustomFilter = filter;
        }
        private void FilterFD()
        {
            string filter = "FDAUFKRS='" + dwABPOS["ABPAUFKRS"].ToString() + "' AND FDAUFLFD='" + dwABPOS["ABPAUFLFD"].ToString() + "' AND FDAUFPOS='" + dwABPOS["ABPAUFPOS"].ToString() + "'";
            cvFDECK.CustomFilter = filter;
        }
        private void cmdLoad_Click(object sender, RoutedEventArgs e)
        {
            DataSet SResult = null;
            try
            {
                DataRow dwData = SetData.ABPOS.NewRow();
                dwData["ABPMANDANT"] = Mandant;
                dwData["ABPART"] = txtABPART.Text.ToString().Trim();
                SResult = m_Data.ABPLoad(dwData);
                SetData.Clear();
                SetData.Merge(SResult);
                SetProd.Merge(SResult);
                SetProd.Clear();
                if (vwABPAUF.View != null)
                {
                    vwABPAUF.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void ABP_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dwABPAUF == null)
                return;
            DataSet SResult = m_Data.LoadABPOS(dwABPAUF);
            SetData.ABPOS.Clear();
            SetData.Merge(SResult);
            if (vwABPOS.View != null)
            {
                vwABPOS.View.MoveCurrentToFirst();
            }
            if (vwFSSTAMM.View != null)
            {
                vwFSSTAMM.View.MoveCurrentToFirst();
            }
        }
        private void RefreshFile()
        {
            int count = 0;
            list01.Items.Clear();
            list02.Items.Clear();
            string filestemp = @"C:\IntexReport\Data\YCAAV25\01Temp";
            string[] files = System.IO.Directory.GetFiles(filestemp);
            foreach (string file in files)
            {
                list01.Items.Add(file.Replace(filestemp + "\\", ""));
                count++;
            }
            filestemp = @"C:\IntexReport\Data\YCAAV25\02Incoming";
            files = System.IO.Directory.GetFiles(filestemp);
            foreach (string file in files)
            {
                list02.Items.Add(file.Replace(filestemp + "\\", ""));
                count++;
            }
            if (count > 0)
            {
                SetProd.Clear();
                SetProd.Merge(SetData.ABPOS);
                m_Data.AB2FB(SetProd);
            }
        }

        private void cmdNewEntry_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetProd.Clear();

                foreach (DataRow row in SetData.ABPOS.Rows)
                {
                    row["ABPZLO"] = LO;
                    row["ABPZLB"] = LB;
                    row["ABPVFAUFLFD"] = LP;
                    row["ABPAENDBEN"] = m_Data.UserID;
                    row["ABPAENDDAT"] = m_Data.date;
                    if (dwFSSTAMM != null)
                    {
                        row["ABPKPROGRP"] = dwFSSTAMM["FSPROGRP"];
                        row["ABPKARTGRP"] = dwFSSTAMM["FSARTGRP"];
                    }
                    decimal ABPSOLLMENGE7 = System.Convert.ToDecimal(row["ABPSOLLMENGE7"].ToString());
                    decimal ABPEALFM = System.Convert.ToDecimal(row["ABPEALFM"].ToString());
                    decimal ABPEAKG = System.Convert.ToDecimal(row["ABPEAKG"].ToString());
                    string ABPERLKZ = row["ABPERLKZ"].ToString();
                    string Color = row["ABPFARBE"].ToString();
                    string Size = row["ABPGROE"].ToString();

                    if (ABPERLKZ == "J")
                        continue;
                    if (ABPSOLLMENGE7 - ABPEALFM - ABPEAKG * 100 < 3)
                    {
                        if (MessageBox.Show("本SKU:" + Color + "-" + Size + " 计划数量"
                            + ABPSOLLMENGE7 / 100 + "已经接收:"
                            + ABPEALFM / 100 + "+" + ABPEAKG
                            + "已经达到，确认则完结此单", "请确认!", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                        {
                            row["ABPERLKZ"] = "J";
                            row["ABPERLDAT"] = m_Data.date;
                        }
                    }
                }
                SetProd.Merge(SetData.ABPOS);
                m_Data.AB2FB(SetProd);
                if (vwABPOS != null)
                {
                    vwABPOS.View.MoveCurrentToNext();
                }
                SetData.ABPOS.Clear();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void txtABPART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                cmdLoad_Click(null, null);
            }
        }

        private void txtQty_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (vwABPOS != null)
            {
                vwABPOS.View.MoveCurrentToNext();
                Qty = "";
            }
            /*
            try
            {
                if (dwABPOS == null)
                    return;
             
                decimal ABPSOLLMENGE7 = System.Convert.ToDecimal(dwABPOS["ABPSOLLMENGE7"].ToString());
                decimal ABPEALFM = System.Convert.ToDecimal(dwABPOS["ABPEALFM"].ToString());
                decimal ABPEAKG = System.Convert.ToDecimal(dwABPOS["ABPEAKG"].ToString());
                string ABPERLKZ = dwABPOS["ABPERLKZ"].ToString();
                if (ABPERLKZ == "J")
                {
                    if (MessageBox.Show("本生产单数量已经达到，确认继续入库?", "请确认", MessageBoxButton.YesNo) == MessageBoxResult.No)
                    {
                        dwABPOS["ABPEAKG"] = 0;
                        return;
                    }
                }
                string Color = dwABPOS["ABPFARBE"].ToString();
                string Size = dwABPOS["ABPGROE"].ToString();

                if (ABPSOLLMENGE7 - ABPEALFM - ABPEAKG * 100 < 3)
                {
                    if (MessageBox.Show("本SKU:" + Color + "-" + Size + " 计划数量"
                        + ABPSOLLMENGE7 / 100 + "已经接收:"
                        + ABPEALFM / 100 +"+"+ ABPEAKG
                        + "已经达到，确认则完结此单", "请确认!", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        dwABPOS["ABPERLKZ"] = "J";
                        dwABPOS["ABPERLDAT"] = m_Data.date;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
              
            }
             * */
        }
    }
}
